let fetch = require("../../utils/fetch");
let app = getApp();
Page({
    data: {
        balanceStatus: false,
        wechatStatus: false,
        template: '',
        imageUrl: app.globalData.imageUrl,
        input_type: true,
        //免密
        balanceType: '',
        isShow: false,
        styleList: ['noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style']


    },
    onLoad: function(options) {
        // 生命周期函数--监听页面加载
        console.log('onLoad', options);
    },
    freePayStatus: function() {
        fetch.loadingText()
        const self = this;
        fetch
            .freePayStatus()
            .then(response => {
                fetch.hideLoadingText();
                const res = response.data;
                // 0：未签约  1：已签约
                if (res.code == 200 && res.obj.WX_APPLET == 1) {
                    this.setData({
                        wechatStatus: true
                    })

                } else {
                    this.setData({
                        wechatStatus: false
                    })
                }
                // 0：未签约,有密码  1：已签约 2未签约,无密码
                if (res.code == 200 && res.obj.BALANCE == 1) {
                    this.setData({
                        balanceStatus: true,
                        balanceType: res.obj.BALANCE
                    })

                } else {
                    this.setData({
                        balanceStatus: false,
                        balanceType: res.obj.BALANCE
                    })
                }

            })
            .catch(error => {
                console.log(error)
                this.setData({
                    wechatStatus: false
                })
                fetch.hideLoadingText();
                fetch.showMyToast(self, error.data.des)
            })
    },
    onShow: function(res) {
        fetch.loadingText()
        this.freePayStatus();
        fetch.compatibilityHandler(wx.navigateToMiniProgram);

    },
    todes: function() {
        if (!this.data.wechatStatus) {
            wx.navigateTo({
                url: '../freePaydes/freePaydes'
            })
        }
    },
    switchChange: function(e) {
        const type = e.currentTarget.dataset.type,
            flag = e.detail.value;
        if (type == 'wechat' && flag) {
            fetch.loadingText({
                title: '签约准备中'
            })
            fetch
                .getFreePayParams()
                .then(response => {
                    fetch.hideLoadingText();
                    console.log('res', response)
                    if (response.data.code == 200) {
                        this.gotoMianmi(response.data.obj)
                    } else {
                        this.setData({
                            wechatStatus: false
                        })
                        fetch.showMyToast(this, response.data.des)
                    }

                })
                .catch(error => {
                    console.log('err', error)
                    fetch.hideLoadingText();
                    this.setData({
                        wechatStatus: false
                    })
                    fetch.showMyToast(this, error.data.des)
                })
        }
        if (type == 'wechat' && !flag) {
            console.log("去取消微信免密支付")
            this.showActionSheet(type)
        }
    },
    showActionSheet: function(type) {
        var self = this;
        wx.showActionSheet({
            itemList: ["关闭免密支付"],
            itemColo: "#4e352f",
            success: function(res) {
                if (res.tapIndex == 0) {
                    if (type == 'wechat') {
                        fetch
                            .rescidFreePay()
                            .then(response => {
                                if (response.data.code == 200) {
                                    self.setData({
                                        wechatStatus: false
                                    })
                                    fetch.showToast({
                                        title: "解约成功"
                                    })
                                } else {
                                    self.setData({
                                        wechatStatus: true
                                    })
                                    reject(response)
                                }
                            })
                            .catch(error => {
                                self.setData({
                                    wechatStatus: true
                                })
                                fetch.showMyToast(self, error.data.des)
                            })
                    }

                } else {
                    self.freePayStatus()
                }

            },
            fail: function(res) {
                self.freePayStatus()

                console.log(res.errMsg)
            }
        })
    },
    gotoMianmi: function(config) {
        console.log(config)
        wx.navigateToMiniProgram({
            appId: 'wxbd687630cd02ce1d',
            path: 'pages/index/index',
            extraData: config,
            success(res) {
                // 成功跳转到签约小程序
                console.log(res)
            },
            fail(res) {
                // 未成功跳转到签约小程序
                fetch.handleFetchError('跳转签约失败')
            },
            compelete(res) {}
        })

    },
    balanceSwitchChange: function(e) {
        const type = e.currentTarget.dataset.type,
            flag = e.detail.value;
        console.log(type, flag)
        var _self = this;
        if (type == 2 && flag) {
            wx.showModal({
                title: '提示',
                content: '需先设置密码后才可开通',
                showCancel: true,
                confirmText: "开通",
                confirmColor: "#ff2f31",
                success: function(res) {
                    if (res.confirm) {
                        getApp().globalData.common.isBalance = true;
                        wx.navigateTo({
                            url: '../balanceLoding/balanceLoding'
                        })
                    } else {
                        _self.setData({
                            balanceStatus: false
                        })
                    }
                }
            })
        } else if (type == 0 && flag) {
            //_self.openBalance();
            _self.setDisabled();
        } else if (!flag) {
            wx.showActionSheet({
                itemList: ["关闭免密支付"],
                itemColo: "#4e352f",
                success: function(res) {
                    console.log("** ** ** ")
                    console.log(res)
                    if (res.cancel) {
                        _self.setData({
                            balanceStatus: true,
                            styleList: ['noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style']

                        })
                    }
                    if (res.tapIndex == 0) {
                        _self.closeBalance();
                    } else {

                        _self.setData({
                            balanceStatus: true
                        })
                    }

                },
                fail: function() {
                    console.log("失败")
                    _self.setData({
                        balanceStatus: true,
                        styleList: ['noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style']

                    })
                }
            })

        }
    },
    openBalance: function() {
        var _self = this;
        wx.request({
            url: getApp().globalData.baseUrl + '/applet/balance/pwd/sign.do',
            method: 'get',
            header: getApp().globalData.header,
            success: function(res) {
                console.log(res)
                if (res.data.code == 200) {
                    _self.setData({
                        isShow: false,
                        balanceStatus: true
                    })
                    fetch.showMyToast(_self, "余额免密开启成功")
                } else {
                    _self.setData({
                        balanceStatus: false,
                        isShow: false
                    })
                }

            }
        })



    },
    closeBalance: function() {
        var _self = this;
        wx.request({
            url: getApp().globalData.baseUrl + '/applet/balance/pwd/rescind.do',
            method: 'get',
            header: getApp().globalData.header,
            success: function(res) {
                if (res.data.code == 200) {
                    _self.setData({
                        balanceStatus: false,
                        balanceType: 0,
                        styleList: ['noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style']

                    })
                    fetch.showToast({
                        title: "解约成功"
                    })
                }
            }

        })


    },
    setDisabled: function() {
        this.setData({
            isShow: true
        })
    },
    getPassWord: function(e) {
        var _self = this;
        console.log(e.detail.value.length)

        var arr = _self.data.styleList
        for (var i = 0; i < 6; i++) {
            if (i <= e.detail.value.length - 1) {

                arr[i] = 'select_style'
            } else {
                arr[i] = 'noSelect_style'
            }
        }
        if (e.detail.value.length == 6) {
            _self.filterPassword(e.detail.value);
        }
        _self.setData({
            styleList: arr
        })
        console.log(_self.data.styleList)


    },
    hideView: function() {
        this.setData({
            input_type: true
        })

    },
    closeView: function() {

        this.setData({
            isShow: false,
            balanceStatus: false,
            styleList: ['noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style']

        })
    },
    filterPassword: function(num) {
        var _self = this;
        wx.request({
            url: getApp().globalData.baseUrl + '/applet/balance/pwd/check.do?password=' + num,
            method: 'post',
            header: getApp().globalData.header,
            success: function(res) {
                if (res.data.code == 200) {
                    _self.openBalance();
                    _self.setData({
                        balanceStatus: true
                    })
                } else {
                    _self.setData({
                        balanceStatus: false,
                        isShow: false,
                        styleList: ['noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style']
                    })
                    fetch.showMyToast(_self, "密码错误")
                }

            }

        })

    }



})